package warthog.scangen;

public class FileStrategy extends ScanStrategy {

	public String variables() {
		return 	
				"  public int tabwidth=4;\n" +
				"  int line, col;\n"+
				"  private static final int BUFSIZ = 1024*4;\n"+
				"  private Reader yy_input;\n"+
				"  private char[] yy_buffer = new char[BUFSIZ];\n"+
				"  private int buflen;\n"
				;
	}

	public String functions(String className) {
			return
					"  void reset(Reader yy_input) throws IOException {\n"+
					"    this.yy_input = yy_input;\n"+
					"    buflen = yy_input.read(yy_buffer);\n"+
					"    line=1; col=0; start = Start.init;\n"+
					"    left=0; mark=0;\n"+
					"  }\n"+
					"  "+className+"(Reader yy_input) throws IOException { reset(yy_input); }\n"+
					"  public void advance() { while (left<mark) switch (yy_buffer[left++]) {\n"+
					"  case '\\t': col=(col/tabwidth+1)*tabwidth; break;\n"+
					"  case '\\r': if(left<mark&&yy_buffer[left]=='\\n')left++;\n"+
					"  case '\\n': col=0; line++; break;\n"+
					"  default: col++;\n"+
					"  }}\n"+
	//				"  public char input() { return yy_buffer[mark++]; }\n"+ // This is broken at buffer bounds or EOF.
					"  public String text() { return new String(yy_buffer, left, mark-left); }\n"+
					"  public String text(int a, int b) { return new String(yy_buffer, left+a, mark-left-a-b); }\n"+
					"  public int length() { return mark-left; }\n"+
					"  public char charAt(int p) { return yy_buffer[left+p]; }\n"
					;
		}

	public String charAt(String index) {
		return "yy_buffer["+index+"]";
	}

	public String boundsTest() {
		return 
				"        if (right >= buflen) {\n"+
				"          // scanner bumped against end of buffer. Are we at EOF?\n"+
				"          if (buflen<BUFSIZ) { // Yes, in this case.\n"+
				"            if (left==right) break scan; else break token;\n"+
				"          }\n"+
				"          // OK, not necessarily. First try to make more room.\n"+
				"          if (left==0) throw new RuntimeException(\"Too long of a token!\");\n"+
				"          // -- Slide uneaten characters to the left\n"+
				"          for (int i=left, j=0; i<buflen; i++,j++) yy_buffer[j] = yy_buffer[i];\n"+
				"          // -- Adjust buffer control variables to match\n"+
				"          buflen -= left; right -= left; mark -= left; left=0;\n"+
				"          // Now can we read any more from the file?\n"+
				"          int more = yy_input.read(yy_buffer, buflen, BUFSIZ-buflen);\n"+
				"          if (more>0) buflen += more; // Got more text...\n"+
				"          else { // We really were at EOF after all...\n"+
				"            if (left==right) break scan; else break token;\n"+
				"          }\n"+
				"        }\n"
				;
	}

	public String preToken() {
		return	"      advance();\n";
	}

	public String toss(String thrown) {
		return "throws IOException" + ((thrown != null) ? ","+thrown : "");
	}

	public String bol() {
		return "col==0";
	}

}
